﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilZ.Dotnet.Ex.Communication.Net.BMQ
{
    /// <summary>
    /// BMQ订阅-请求消息
    /// </summary>
    internal class BMQSubReqMessage : BMQSessionMessageAbs
    {
        /****************************************************
         * BMQ订阅-请求消息结构
         * 字段       数据类型    大小(byte)  说明
         * Type       byte        1           BMQ消息类型
         * ConttexId  Int16       2           上下文会话标识
         * Topic      Int16       2           主题
         * 
         ****************************************************/


        /// <summary>
        /// 消息大小
        /// </summary>
        private const int SIZE = 5;

        private short _topic;
        /// <summary>
        /// 订阅主题
        /// </summary>
        public short Topic
        {
            get { return _topic; }
        }


        /// <summary>
        /// 构造函数-解析
        /// </summary>
        public BMQSubReqMessage()
            : base(BMQMessageTypes.SubReq, SIZE)
        {

        }


        /// <summary>
        /// 构造函数-创建
        /// </summary>
        /// <param name="topic">订阅主题</param>
        public BMQSubReqMessage(short topic)
            : base(BMQMessageTypes.SubReq, SIZE, BMQSessionIdGenerator.Generate())
        {
            this._topic = topic;
        }


        /// <summary>
        /// 将消息写入二进制buffer
        /// </summary>
        /// <param name="writer">消息写器</param>
        protected override void WriteBytes(BinaryWriter writer)
        {
            writer.Write(this._topic);
        }

        /// <summary>
        /// 读取消息到对象
        /// </summary>
        /// <param name="reader">消息读取器</param>
        protected override void PrimitiveFromBytes(BinaryReader reader)
        {
            this._topic = reader.ReadInt16();
        }
    }
}
